package com.noble.glamour.module.logic.baseui.custom;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.widget.AppCompatTextView;
import android.util.AttributeSet;

/**
 * 逐个显示文字的TextView
 * <p>
 * Created by Su on 2017/3/20.
 */
public class SinglyTextView extends AppCompatTextView implements Handler.Callback {
    private Handler mHandler;
    private final int SHOW_NEXT_CHAR = 1;

    private String mOriginalStr;    // TextView展示字符串
    private long mDuration = 600;   // 默认显示每个字符的时间间隔
    private int mIndex = 0;         // 记录将要显示的字符的位置
    private int mRepeatLimit = -1;  // 循环展示次数，默认无限循环

    public SinglyTextView(Context context) {
        super(context);
        init();
    }

    public SinglyTextView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public SinglyTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        mOriginalStr = getText().toString();
        Looper looper;
        if ((looper = Looper.myLooper()) != null) {
            mHandler = new Handler(looper, this);
        } else if ((looper = Looper.getMainLooper()) != null) {
            mHandler = new Handler(looper, this);
        } else {
            mHandler = null;
        }
        startRepeat();
    }

    @Override
    public boolean handleMessage(Message msg) {
        if (msg.what == SHOW_NEXT_CHAR && mIndex < mOriginalStr.length()) {
            this.setText(SinglyTextView.this.getText().toString()
                    + mOriginalStr.charAt(mIndex));
            mIndex++;
            mHandler.sendEmptyMessageDelayed(SHOW_NEXT_CHAR, mDuration);
            return false;
        }

        if (mIndex >= mOriginalStr.length()) {
            if (mRepeatLimit == -1) {
                startRepeat();
            } else if (mRepeatLimit > 0) {
                mRepeatLimit--;
                startRepeat();
            }
        }
        return false;
    }

    /**
     * 启动文字循环
     */
    private void startRepeat() {
        this.setText("");
        mIndex = 0;
        stopRepeat();
        if (mHandler != null)
            mHandler.sendEmptyMessageDelayed(SHOW_NEXT_CHAR, mDuration);
    }

    /**
     * 关闭文字循环
     */
    private void stopRepeat() {
        if (mHandler != null)
            mHandler.removeMessages(SHOW_NEXT_CHAR);
    }

    /**
     * 设置显示每个字符的时间间隔
     */
    public void setDuration(long duration) {
        mDuration = duration;
    }

    /**
     * 设置动画循环次数
     */
    public void setRepeatLimit(int repeatLimit) {
        mRepeatLimit = repeatLimit;
    }

    private void release() {
        stopRepeat();
        mHandler = null;
    }

    /**
     * 销毁view时做释放操作
     */
    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        release();
    }
}
